package io.afu.nginxhelper.utils;

import io.afu.nginxhelper.constant.NgxConst;
import io.afu.nginxhelper.dto.ListResult;

import java.util.ArrayList;
import java.util.List;

public class StringUtils {


    public static boolean isNotEmpty(String str){
        return null != str && !"".equals(str);
    }

    public static List<String> splitCleanStr(String str,String sep) {
        List<String> cleanStrList = new ArrayList<>();
        String cleanStr = str.trim();
        String[] strs = cleanStr.split(sep);
        for (String theStr:strs){
            if (isNotEmpty(theStr.trim())){
                cleanStrList.add(theStr);
            }
        }
        return cleanStrList;
    }


    public static ListResult getBlockByStartMark(List<String> wholeLineList, String mark) {
        ListResult listResult = new ListResult();
        listResult.setWhole(wholeLineList);
        int blockStart = 0;
        int blockEnd = 0;
        int startline = -1;
        int endline = -1;
        for (int i = 0;i<wholeLineList.size();i++){
            if (wholeLineList.get(i).contains(mark)){
                startline = i;
            }
            if (startline != -1){
                if (wholeLineList.get(i).contains(NgxConst.BLOCK_START)){
                    blockStart+=1;
                }
                if (wholeLineList.get(i).contains(NgxConst.BLOCK_END)){
                    blockEnd+=1;
                    if (blockStart == blockEnd){
                        endline = i;
                        break;
                    }
                }
            }
        }
        listResult.setTarget(new ArrayList<>(wholeLineList.subList(startline,endline+1)));
        List<String> left = new ArrayList<>(wholeLineList.subList(0,startline));
        List<String> tmp = new ArrayList<>(wholeLineList.subList(endline+2,wholeLineList.size()));
        left.addAll(tmp);
        listResult.setLeft(left);
        return listResult;
    }



}
